<?php
/**
 * Service_Declination
 *
 * Класс Service_Declination отвечает за склонения слов и фраз
 *
 */
class Service_Declination {

	
  /**
   * Получает родительный падеж слова/словосочетания
   * 
   * @param string $strWord - слово/словосочетания, которое надо просклонять
   * 
   *  @return string
   */	
	public static function getGenitive($strWord) {
	  if (strpos($strWord, ' ')) { // Если словосочетание (есть пробелы)
	    $arrWords = explode(' ', $strWord);
	    $retStr = '';
	    foreach ($arrWords as $word) {
	    	if (!empty($retStr)) $retStr .= ' ';
	    	$retStr .= self::getGenitive($word);
	    }
	    return $retStr;
	  }
		switch ($strWord) {
	  	case 'Ростов-на-Дону': return 'Ростова-на-Дону';
	  	default:
	  		$wordLength = Service_Function::utf8_strlen($strWord);
			  $lastLetter = Service_Function::utf8_substr($strWord, $wordLength - 1);
			  $strWordWithoutLetter = Service_Function::utf8_substr($strWord, 0, $wordLength - 1);
			  switch ($lastLetter) {
  				// Обрабатываемый алфафит
  				//авгджкрья
			  	case 'а':
					  $lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ка': return  $strWordWithoutLetters . 'ки';
							case 'га': return  $strWordWithoutLetters . 'ги';
							default: return $strWordWithoutLetter . 'ы';
						}
					case 'к':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ок': return  $strWordWithoutLetters . 'ка';
							default: return $strWordWithoutLetter . 'ка';
						}
					case 'т':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ст': return  $strWordWithoutLetters . 'ста';
							default: return $strWordWithoutLetter . 'т';
						}
					
					case 'в': case 'г': case 'д': case 'ж': case 'н': case 'р': case 'щ': case 'л' :
						return $strWord . 'а';
					case 'е':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ое': return  $strWordWithoutLetters . 'ого';
							case 'ье': return  $strWordWithoutLetters . 'ья';
							case 'ые': return  $strWordWithoutLetters . 'ых';
							default: return $strWordWithoutLetter . 'е';
						}
					case 'ы':
						return $strWordWithoutLetter;
					case 'ь':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ль': return  $strWordWithoutLetters . 'ля';
							default: return $strWordWithoutLetter . 'и';
						}
					case 'я':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ая': return  $strWordWithoutLetters . 'ой';
							default: return $strWordWithoutLetter . 'и';
						}						
					case 'й':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ий':
								$last3Letters = Service_Function::utf8_substr($strWord, $wordLength - 3);
								switch ($last3Letters) {
									case 'кий': return $strWordWithoutLetters . 'ого';
									default: return  $strWordWithoutLetters . 'его';
								}
							case 'ый':
								$last3Letters = Service_Function::utf8_substr($strWord, $wordLength - 3);
								switch ($last3Letters) {
									case 'ный': return $strWordWithoutLetters . 'ого';
									default: return  $strWordWithoutLetters;
								}
							default: return $strWord;
						}
					default: return $strWord;
			  }
	  		break;
	  }	
	}
	
	public static function getDative($strWord) {
		if (strpos($strWord, ' ')) { // Если словосочетание (есть пробелы)
			$arrWords = explode(' ', $strWord);
			$retStr = '';
			foreach ($arrWords as $word) {
				if (!empty($retStr)) $retStr .= ' ';
				$retStr .= self::getDative($word);
			}
			return $retStr;
		}
		switch ($strWord) {
			case 'Ростов-на-Дону': return 'Ростову-на-Дону';
			default:
				$wordLength = Service_Function::utf8_strlen($strWord);
				$lastLetter = Service_Function::utf8_substr($strWord, $wordLength - 1);
				$strWordWithoutLetter = Service_Function::utf8_substr($strWord, 0, $wordLength - 1);
				switch ($lastLetter) {
					// Обрабатываемый алфафит
					//авгджкрья
					case 'а':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ка': return  $strWordWithoutLetters . 'ки';
							default: return $strWordWithoutLetter . 'ы';
						}
					case 'к':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ок': return  $strWordWithoutLetters . 'ка';
							default: return $strWordWithoutLetter . 'ка';
						}
					case 'в': case 'г': case 'д': case 'ж': case 'н': case 'р': case 'щ':
						return $strWord . 'у';
					case 'е':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ое': return  $strWordWithoutLetters . 'ого';
							case 'ье': return  $strWordWithoutLetters . 'ья';
							case 'ые': return  $strWordWithoutLetters . 'ых';
							default: return $strWordWithoutLetter . 'е';
						}
					case 'ы':
						return $strWordWithoutLetter;
					case 'ь':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ль': return  $strWordWithoutLetters . 'ля';
							default: return $strWordWithoutLetter . 'и';
						}
					case 'я':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ая': return  $strWordWithoutLetters . 'ой';
							default: return $strWordWithoutLetter . 'и';
						}
					case 'й':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ий':
								$last3Letters = Service_Function::utf8_substr($strWord, $wordLength - 3);
								switch ($last3Letters) {
									case 'кий': return $strWordWithoutLetters . 'ому';
									default: return  $strWordWithoutLetters . 'его';
								}
							case 'ый':
								$last3Letters = Service_Function::utf8_substr($strWord, $wordLength - 3);
								switch ($last3Letters) {
									case 'ный': return $strWordWithoutLetters . 'ому';
									default: return  $strWordWithoutLetters;
								}
							default: return $strWord;
						}
					default: return $strWord;
				}
				break;
		}
	}
	
	public static function getAccusative() {
		
	}
	
	public static function getAblative() {
		
	}
	
	public static function getPrepositional($strWord) {
		
	  if (strpos($strWord, ' ')) { // Если словосочетание (есть пробелы)
	    $arrWords = explode(' ', $strWord);
	    $retStr = '';
	    foreach ($arrWords as $word) {
	    	if (!empty($retStr)) $retStr .= ' ';
	    	$retStr .= self::getPrepositional($word);
	    }
	    return $retStr;
	  }
		switch ($strWord) {
	  	case 'Ростов-на-Дону': return 'Ростове-на-Дону';
	  	default:
	  		$wordLength = Service_Function::utf8_strlen($strWord);
			  $lastLetter = Service_Function::utf8_substr($strWord, $wordLength - 1);
			  $strWordWithoutLetter = Service_Function::utf8_substr($strWord, 0, $wordLength - 1);
			  switch ($lastLetter) {
  				// Обрабатываемый алфафит
  				//авгджкрья
			  	case 'а':
						return $strWordWithoutLetter . 'е';
					case 'в': case 'г': case 'д': case 'ж': case 'к': case 'н': case 'р': case 'щ':
						return $strWord . 'е';
					case 'е':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ое': return  $strWordWithoutLetters . 'ом';
							case 'ье': return  $strWordWithoutLetters . 'ье';
							case 'ые': return  $strWordWithoutLetters . 'ых';
							default: return $strWordWithoutLetter . 'е';
						}
					case 'ы':
						return $strWordWithoutLetter . 'ах';
					case 'ь':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ль': return  $strWordWithoutLetters . 'ле';
							default: return $strWordWithoutLetter . 'и';
						}
					case 'я':
			  		$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ая': return  $strWordWithoutLetters . 'ой';
							default: return $strWordWithoutLetter . 'и';
						}						
					case 'й':
						$lastLetters = Service_Function::utf8_substr($strWord, $wordLength - 2);
						$strWordWithoutLetters = Service_Function::utf8_substr($strWord, 0, $wordLength - 2);
						switch ($lastLetters) {
							case 'ий':
								$last3Letters = Service_Function::utf8_substr($strWord, $wordLength - 3);
								switch ($last3Letters) {
									case 'кий': return $strWordWithoutLetters . 'ом';
									default: return  $strWordWithoutLetters . 'ем';
								}
							case 'ый': return  $strWordWithoutLetters . 'ом';
							default: return $strWord;
						}
					default: return $strWord;
			  }
	  		break;
	  }	
	}
	
	/**
   * Определяем склоенение
   *
   * @param array $number - число для которого ищем склонение
   */
	public static function getAmountDeclination($number) {
		$number = $number % 100;
		if (($number > 10) && ($number < 20)) return 1; // Родительный, множественное число
		else{
			switch ($number % 10) {
				case 1: return 2; // Именительный, единственное число
				case 2:	case 3:	case 4:	return 3; // Именительный, множественное число
				case 5: case 6:	case 7:	case 8:	case 9:	case 0:	return 1; // Родительный, множественное число
				default: return 1;
			}
		}
	}
	
	/**
	 * Склонение лет
	 *
	 * @param $number - число
	 * @return
	 */
	public static function getYearsDeclension($number) {
		switch (self::getAmountDeclination($number)) {
			case 1: return ' лет ';
			case 2: return ' год ';
			case 3:	return ' года ';
		}
	}
	
	/**
	 * Склонение ночей
	 *
	 * @param $number - число
	 * @return
	 */
	public static function getNightsDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return 'nights';
			else return 'night';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return 'ночей';
			case 2: return 'ночь';
			case 3:	return 'ночи';
		}
	}
	
	/**
	 * Склонение городов
	 *
	 * @param $number - число
	 * @return
	 */
	public static function getCitiesDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return ' cities ';
			else return ' city ';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return ' городов ';
			case 2: return ' город ';
			case 3:	return ' города ';
		}
	}
	
	/**
	 * Склонение Отелей
	 *
	 * @param $number - число
	 * @return
	 */
	public static function getOtelsDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return ' hotels ';
			else return ' hotel ';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return ' отелей ';
			case 2: return ' отель ';
			case 3:	return ' отеля ';
		}
	}
	
	
	/**
	 * Склонение Гостиниц
	 *
	 * @param $number - число
	 * @return
	 */
	public static function getHotelsDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return ' hotels ';
			else return ' hotel ';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return ' гостиниц ';
			case 2: return ' гостиница ';
			case 3:	return ' гостиницы ';
		}
	}
	
	/**
	 * Склонение номеров
	 *
	 * @param $number - число
	 * @return string
	 */
	public static function getRoomsDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return 'rooms';
			else return 'room';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return 'номеров';
			case 2: return 'номер';
			case 3:	return 'номера';
		}
	}
	/**
	 * Склонение детей
	 *
	 * @param $number - число
	 * @return string
	 */
	public static function getChildrenDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return 'children';
			else return 'child';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: return 'детей';
			case 2: return 'ребенок';
			case 3:	return 'ребенка';
		}
	}
	/**
	 * Склонение людей
	 *
	 * @param $number - число
	 * @return string
	 */
	public static function getPeopleDeclension($number) {
		if (Service_Translate::getLang() == 'en') {
			if ($number > 1) return 'people';
			else return 'man';
		}
		switch (self::getAmountDeclination($number)) {
			case 1: case 2: return 'человек';
			case 3:	return 'человека';
		}
	}
	
	

}
?>